/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("PrintTemplate",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using Mercury.Entity.DomainModels;
using Mercury.System.IServices;
using Mercury.Core.Utilities;
using Mercury.Core.Filters;

namespace Mercury.System.Controllers
{
    public partial class PrintTemplateController
    {
        private readonly IPrintTemplateService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public PrintTemplateController(
            IPrintTemplateService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        [Route("GetTemplate/{table}")]
        [HttpGet]
        public async Task<ActionResult> GetTemplate(string table)
        {
            var entity = await _service.GetEntity(c => c.TableName.Trim().ToLower() == table.Trim().ToLower());
            return Json(new { status = true, data = entity });
        }


        [Route("SaveTemplate")]
        [HttpPost]
        [ApiActionPermission(ActionRolePermission.SuperAdmin)]
        public WebResponseContent SaveTemplate([FromBody] PrintTemplate template)
        {
            if (template.Id > 0)
            {
                return _service.UpdateEntity(template);
            }
            else
            {
                return _service.AddEntity(template, false);
            }
        }
    }
}
